﻿#include "dlgfault.h"
#include "ui_dlgfault.h"

#include <QDebug>
#include <QKeyEvent>

DlgFault::DlgFault(const QString &faultCode, QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DlgFault),
    m_faultCode(faultCode)
{
    ui->setupUi(this);

    // 初始化
    initUI();
    init();
}

DlgFault::~DlgFault()
{
    delete ui;
}

bool DlgFault::eventFilter(QObject *watched, QEvent *event)
{
    if (watched == ui->btnYes || watched == ui->btnNo) {
        if (event->type() == QEvent::KeyPress) {
            QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
            if (keyEvent->key() == Qt::Key_Escape) {
                return true;
            }
            if (keyEvent->key() == Qt::Key_Space) {
                return true;
            }
            if (keyEvent->key() == Qt::Key_Return) {
                return true;
            }
            if (keyEvent->key() == Qt::Key_Enter) {
                return true;
            }
        }
    }

    return QDialog::eventFilter(watched, event);
}

void DlgFault::initUI()
{
    setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::MSWindowsFixedSizeDialogHint);
    ui->btnYes->installEventFilter(this);
    ui->btnNo->installEventFilter(this);

    connect(ui->btnYes,  &QPushButton::clicked, this, &DlgFault::onYes);
    connect(ui->btnNo, &QPushButton::clicked, this, &DlgFault::onNo);
}

void DlgFault::init()
{
    QString question = QStringLiteral("问题: ");
    if (QString::compare(m_faultCode, "C15101010011") == 0) {
        question = QStringLiteral("1553B总线是冗余形式吗?");
    } else if (QString::compare(m_faultCode, "C15102010011") == 0) {
        question = QStringLiteral("1553B总线是冗余形式吗?");
    } else if (QString::compare(m_faultCode, "C15101010041") == 0) {
        question = QStringLiteral("伺服作动器是否相互独立?");
    } else if (QString::compare(m_faultCode, "C15102010041") == 0) {
        question = QStringLiteral("伺服作动器是否相互独立?");
    } else if (QString::compare(m_faultCode, "C15101030051") == 0) {
        question = QStringLiteral("伺服系统标称供电电压是28伏吗?");
    } else if (QString::compare(m_faultCode, "C15102030051") == 0) {
        question = QStringLiteral("伺服系统标称供电电压是28伏吗?");
    } else {
    }

    ui->lblQuestion->setText(question);
}

void DlgFault::onYes()
{
    this->accept();
}

void DlgFault::onNo()
{
    this->reject();
}
